{% extends "base.html" %}

{% block title %}编辑任务 - CronPy Web管理界面{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header">
                <h4 class="mb-0"><i class="fas fa-edit me-2"></i>编辑任务 - {{ task_id }}</h4>
            </div>
            <div class="card-body">
                <form method="POST">
                    {{ form.hidden_tag() }}

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                {{ form.task_id.label(class="form-label") }}
                                {{ form.task_id(class="form-control", readonly=True) }}
                                <div class="form-text">
                                    <small class="text-muted">任务ID不可修改</small>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                {{ form.name.label(class="form-label") }}
                                {{ form.name(class="form-control", placeholder="例如: 数据库备份") }}
                                {% if form.name.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.name.errors %}{{ error }}{% endfor %}
                                    </div>
                                {% endif %}
                            </div>
                        </div>
                    </div>

                    <div class="mb-3">
                        {{ form.cron_expression.label(class="form-label") }}
                        <div class="input-group">
                            {{ form.cron_expression(class="form-control", placeholder="例如: 0 2 * * *") }}
                            <a href="{{ url_for('cron_examples') }}" class="btn btn-outline-secondary" target="_blank">
                                <i class="fas fa-question-circle"></i>
                            </a>
                        </div>
                        {% if form.cron_expression.errors %}
                            <div class="invalid-feedback d-block">
                                {% for error in form.cron_expression.errors %}{{ error }}{% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">
                            <small class="text-muted">
                                Cron表达式格式: * * * * * * (秒 分 时 日 月 周)
                                <a href="{{ url_for('cron_examples') }}" target="_blank">查看示例</a>
                            </small>
                        </div>
                    </div>

                    <div class="mb-3">
                        {{ form.command.label(class="form-label") }}
                        {{ form.command(class="form-control", rows=3, placeholder="例如: python backup.py --full") }}
                        {% if form.command.errors %}
                            <div class="invalid-feedback d-block">
                                {% for error in form.command.errors %}{{ error }}{% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">
                            <small class="text-muted">要执行的命令，支持绝对路径和环境变量</small>
                        </div>
                    </div>

                    <div class="mb-3">
                        {{ form.description.label(class="form-label") }}
                        {{ form.description(class="form-control", rows=2, placeholder="任务的详细描述...") }}
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                {{ form.working_dir.label(class="form-label") }}
                                {{ form.working_dir(class="form-control", placeholder="例如: C:\\backup 或 /home/user/backup") }}
                                <div class="form-text">
                                    <small class="text-muted">命令执行的工作目录，留空使用当前目录</small>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-3">
                                {{ form.timeout.label(class="form-label") }}
                                {{ form.timeout(class="form-control", type="number", min="0", placeholder="0") }}
                                <div class="form-text">
                                    <small class="text-muted">超时时间(秒)，0表示不限制</small>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-3">
                                {{ form.max_retries.label(class="form-label") }}
                                {{ form.max_retries(class="form-control", type="number", min="0", placeholder="0") }}
                                <div class="form-text">
                                    <small class="text-muted">执行失败时的最大重试次数</small>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            {{ form.enabled(class="form-check-input") }}
                            {{ form.enabled.label(class="form-check-label") }}
                        </div>
                        <div class="form-text">
                            <small class="text-muted">启用后任务将按照Cron表达式自动执行</small>
                        </div>
                    </div>

                    <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                        <a href="{{ url_for('index') }}" class="btn btn-secondary me-md-2">
                            <i class="fas fa-times me-1"></i>取消
                        </a>
                        <button type="submit" class="btn btn-primary" onclick="addTimestampToForm()">
                            <i class="fas fa-save me-1"></i>保存更改
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
function addTimestampToForm() {
    const form = document.querySelector('form');
    if (form) {
        // 检查是否已经有时间戳参数
        const timestampInput = form.querySelector('input[name="_t"]');
        if (!timestampInput) {
            // 创建隐藏的时间戳输入框
            const input = document.createElement('input');
            input.type = 'hidden';
            input.name = '_t';
            input.value = new Date().getTime();
            form.appendChild(input);
        } else {
            // 更新现有时间戳
            timestampInput.value = new Date().getTime();
        }
    }
}
</script>
{% endblock %}
